﻿//used for menu character which has qulif
function rebuildMenuParam(ta, param) {
    var t = ta.attr('qualification');
    var reg = /\$\d+\$/;
    var a = reg.exec(t);
    if (a != null) {
        for (i = 0; i < a.length; i++) {
            var id = a[i].replace('$', '').replace('$', '');
            //        var idv = $('#' + id).next().children('input[type=hidden]').val();
            var idv = $('#' + id).combo('getValue');
            var dtv = $('#' + id).attr('artype');
            if (dtv == "IntegerField" || dtv == "RealField" || dtv == "DecimalField")
                t = t.replace(a[i], idv);
            else
                t = t.replace(a[i], '"' + idv + '"');
        }
    }
    param.qu = t;
}


function rebuildTableParam(ta, param) {


    var reg = /\$\d+\$/;
    var t = ta.attr('qualification');
    var regexternal = /EXTERNAL\([^)]+\)/g;
    //var a = regexternal.exec(t);
    var a = t.match(regexternal);
    if (a != null) {
    //how many external(...)
        for (i = 0; i < a.length; i++) {
            var k = reg.exec(a[i]);
            if (k != null) {
                //for (j = 0; j < k.length; j++) {
                    var id = k[0].replace('$', '').replace('$', '');
                    var idv = $('#' + id).val();
                    if (idv == null || idv == undefined || idv == "")
                        idv = "1=1";
                    t = t.replace(a[i], idv);
                //}
            }
//            var id = a[i].replace('$', '').replace('$', '');
//            //        var idv = $('#' + id).next().children('input[type=hidden]').val();
//            var idv = $('#' + id).combo('getValue');
//            var dtv = $('#' + id).attr('artype');
//            if (dtv == "IntegerField" || dtv == "RealField" || dtv == "DecimalField")
//                t = t.replace(a[i], idv);
//            else
//                t = t.replace(a[i], '"' + idv + '"');
        }
    }

    var b = reg.exec(t);
    if (b != null) {
        for (i = 0; i < b.length; i++) {
            var id = b[i].replace('$', '').replace('$', '');
            //        var idv = $('#' + id).next().children('input[type=hidden]').val();
            var idv = $('#' + id).val();
            var dtv = $('#' + id).attr('artype');
            if (dtv == "IntegerField" || dtv == "RealField" || dtv == "DecimalField")
                t = t.replace(b[i], idv);
            else
                t = t.replace(b[i], '"' + idv + '"');
        }
    }

    param.qu = t;
}

//copy for each audit table
//function audit_style(value, row, index) {
//    if (index > 0) {
//        //check browser capability
//        if (value != $('#table1').datagrid('getData').rows[index - 1][this.field])
//            return 'background-color:#ffee00;color:red;';
//    }
//}



function showError(data) {

    $('#error_dlg').dialog('body').children().children().append($("<p></p>").html(data.Exception));
    $('#error_dlg').dialog('open');
    
}


function auto_err_Loader(param, success, error) {
    var that = $(this);
    var opts = that.datagrid("options");
    if (!opts.url) {
        return false;
    }
    $.ajax({
        type: opts.method,
        url: opts.url,
        data: param,
        dataType: "json",
        success: function (data) {
            if ('Exception' in data) {
                showError(data);
            }
            success(data);
        },
        error: function () {
            error.apply(this, arguments);
        }
    });
}


(function($) { 
    $(document).ready(function(){
        $('.commit-change-mod').bind('click',function(){
            
            var f = $(this).parentsUntil('form').parent();
            if(f != null)
            {
                var action = $(this).attr("action");
                var form = $("<form></form>");
                form.attr('action',action);
                form.attr('method','post');
                form.css('display','none');
                //enumerate input
                f.find('input[arname]').each(function(index,object){

                    var old=$(object).attr('oval');
                    var name = $(object).attr('arname');

                    var v = $("input[name=" + name + "]").val();
                    //input的值改变才执行下面
                    if(v != undefined && old != v){
                        var input = $("<input type='hidden' />");
                        input.attr('name',name);
                        input.attr('value',v);
                        form.append(input);
                    }
                });

                //enumerate textarea
                f.find('textarea[arname]').each(function(index,object){
            
                    var old=$(object).attr('oval');
                    var name = $(object).attr('arname');

                    var v = $("textarea[name=" + name + "]").val();

                    if(v != undefined && old != v){
                        var textarea = $("<textarea></textarea>");
                        textarea.attr('name',name);
                        textarea.text($(object).val());
                        form.append(textarea);
                    }
                });

                var input = $("<input type='hidden'/>");

                input.attr('name','buttonclick');
                //input.attr('name',$(this).attr('name'));
                input.attr('value',$(this).attr('name'));
               
                form.append(input);//submit button self

                
                var input2 = $("<input type='hidden'/>");
                input2.attr('name','requestid');
                input2.attr('value',$('#requestid').val());
                form.append(input2);//submit button self

                form.appendTo("body");
                form.submit();
            }
        });


        $('.commit-change-search').bind('click',function(){
            
            var button = $(this);

            $('form').each(function(){
                var action = button.attr("action");
                var form = $("<form></form>");
                form.attr('action',action);
                form.attr('method','post');
                form.css('display','none');
                //enumerate input
                $(this).find('input[arname]').each(function(index,object){

                    var old=$(object).attr('oval');
                    var name = $(object).attr('arname');
                    var id = $(object).attr('id');
                    var v = $("input[name=" + name + "]").val();

                    if(v != undefined && old != v){
                        var input = $("<input type='hidden' />");
                        input.attr('name',id);
                        input.attr('value',v);
                        form.append(input);
                    }
                });

                //enumerate textarea
                $(this).find('textarea[arname]').each(function(index,object){
            
                    var old=$(object).attr('oval');
                    var name = $(object).attr('arname');
                    var id = $(object).attr('id');
                    var v = $("textarea[name=" + name + "]").val();

                    if(v != undefined && old != v){
                        var textarea = $("<textarea></textarea>");
                        textarea.attr('name',id);
                        textarea.text($(object).val());
                        form.append(textarea);
                    }
                });
                form.appendTo("body");
                form.submit();
            });
        });


        //error dialog init
        $('#error_dlg').dialog({
            title: ' ',  
            width: 400,  
            height: 200,  
            cache: false,  
            closed:true, 
            modal: true,
            iconCls:'icon-cancel',
            onClose:function(){
                $(this).children().children().html('');
            }
        });


    });
})(jQuery);
